var Consts = require('../../util/consts');
var killerUserDao = require('../../dao/killer/killerUserDao');
var killerRoomDao = require('../../dao/killer/killerRoomDao');
var utils = require('../../util/utils');
var playerStateService = require('../playerStateService');


var gameService = module.exports;

gameService.GameStatus = {
    GO_ON: 0,
    KILLER_WIN: 1,
    COMMONER_WIN: 2
};

var admin = 'backgroundAdmin';

gameService.checkGameStatus = function (rid, cb) {
    var self = this;
    killerUserDao.getAliveUsersByRidAndRole(rid, Consts.KillerRole.KILLER, function (err, killers) {
        if (err) {
            console.error("check game status error");
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null, null);
        } else {
            var code = self.GameStatus.GO_ON; // 游戏继续
            if (killers.length == 0) {
                killerRoomDao.closeRoom(rid, function (err, room) {
                    self._updatePlayerState(rid, function (notUsed) {
                        // killers win
                        code = self.GameStatus.COMMONER_WIN;
                        killerUserDao.getUsersByRidAndRole(rid, Consts.KillerRole.COMMONER, function (err, commoners) {
                            if (err) {
                                console.error("server error");
                            }
                            utils.invokeCallback(cb, null, code, commoners);
                        });
                    });
                });


            } else {
                killerUserDao.getAliveUsersByRidAndRole(rid, Consts.KillerRole.COMMONER, function (err, commoners) {
                    if (err) {
                        console.error("check game status error");
                        utils.invokeCallback(cb, {code: err.number, msg: err.message}, null, null);
                    } else {
                        if (commoners.length == 0) {
                            killerRoomDao.closeRoom(rid, function (err, room) {
                                self._updatePlayerState(rid, function (notUsed) {
                                    // commoners win
                                    code = self.GameStatus.KILLER_WIN;
                                    killerUserDao.getUsersByRidAndRole(rid, Consts.KillerRole.KILLER, function (err, killers) {
                                        utils.invokeCallback(cb, null, code, killers);
                                    });
                                });
                            });

                        } else {
                            utils.invokeCallback(cb, null, code, []);
                        }
                    }
                });
            }
        }
    });
};

gameService._updatePlayerState = function (rid, cb) {
    killerUserDao.getUsersByRid(rid, function (err, users) {
        for (var i = 0; i < users.length; i++) {
            var user = users[i];
            var name = user.name;
            playerStateService.outKillerGame(name, '', cb);
        }
    });
};